#include "batchmodify.h"
#include "ui_batchmodify.h"

#include <QMessageBox>

BatchModify::BatchModify(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BatchModify)
{
    ui->setupUi(this);
    //使每次弹窗都在最前面
    setWindowFlag(Qt::WindowStaysOnTopHint);

    tablewidgetTitle<<"序号"<<"书名"<<"类型"<<"标签类型"<<"全路径"<<"路径"<<"作者"<<"出版社"<<"出版年份"<<"ISBN号"<<"简介"; //表格标题固定
    tablewidgetColumnCount = tablewidgetTitle.size(); //表格列数
    ui->tableWidgetModify->setMouseTracking(true);  //鼠标追踪，鼠标悬停到单元格上时显示内容，也可在ui中设置

    //鼠标追踪，鼠标悬停在单元格上时，显示单元格内容
    connect(ui->tableWidgetModify, SIGNAL(entered(QModelIndex)),this,SLOT(tabelTooltip(QModelIndex)));
}

BatchModify::~BatchModify()
{
    delete ui;
}

void BatchModify::tableDataShow(QList<EbookInfo> bookInfoList)
{
    int rowNum = bookInfoList.size();
    ReadOnlyDelegate *readOnlyDelegate = new ReadOnlyDelegate(this);

    ui->tableWidgetModify->setRowCount(0);
    ui->tableWidgetModify->setColumnCount(0);
    ui->tableWidgetModify->clearContents();
    ui->tableWidgetModify->clear();
    ui->tableWidgetModify->setColumnCount(tablewidgetColumnCount);
    ui->tableWidgetModify->setHorizontalHeaderLabels(tablewidgetTitle);
    ui->tableWidgetModify->setItemDelegateForColumn(0, readOnlyDelegate); //设置第0列不可编辑
    ui->tableWidgetModify->setItemDelegateForColumn(1, readOnlyDelegate);
    ui->tableWidgetModify->setItemDelegateForColumn(2, readOnlyDelegate);
    ui->tableWidgetModify->setItemDelegateForColumn(4, readOnlyDelegate);
    ui->tableWidgetModify->setItemDelegateForColumn(5, readOnlyDelegate);
    ui->tableWidgetModify->setColumnWidth(0,38);
    ui->tableWidgetModify->setColumnWidth(1,380);

    //根据数量将数据显示
    ui->tableWidgetModify->setRowCount(rowNum);
    for (int i = 0; i < bookInfoList.size(); ++i)
    {
        ui->tableWidgetModify->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidgetModify->setItem(i,1,new QTableWidgetItem(bookInfoList[i].bookname));
        ui->tableWidgetModify->setItem(i,2,new QTableWidgetItem(bookInfoList[i].booktype));
        ui->tableWidgetModify->setItem(i,3,new QTableWidgetItem(bookInfoList[i].booklabel));
        ui->tableWidgetModify->setItem(i,4,new QTableWidgetItem(bookInfoList[i].bookpath));
        ui->tableWidgetModify->setItem(i,5,new QTableWidgetItem(bookInfoList[i].bookdir));
        ui->tableWidgetModify->setItem(i,6,new QTableWidgetItem(bookInfoList[i].author));
        ui->tableWidgetModify->setItem(i,7,new QTableWidgetItem(bookInfoList[i].publisher));
        ui->tableWidgetModify->setItem(i,8,new QTableWidgetItem(bookInfoList[i].publishyear));
        ui->tableWidgetModify->setItem(i,9,new QTableWidgetItem(bookInfoList[i].bookisbn));
        ui->tableWidgetModify->setItem(i,10,new QTableWidgetItem(bookInfoList[i].bookintroduction));
    }

    ui->labelShowNum->setText(QString("数目：%1").arg(rowNum));
}

//鼠标悬停在单元格中时显示单元格内容
void BatchModify::tabelTooltip(QModelIndex index)
{
    QToolTip::showText(QCursor::pos(),index.data().toString());
}

void BatchModify::on_ptnConfirmModify_clicked()
{
    QMessageBox msgBox(QMessageBox::Information, tr("信息"),tr("确定要保存修改吗？")
                       ,QMessageBox::Yes|QMessageBox::No,this);
    if (msgBox.exec() == QMessageBox::Yes)
    {
        QList<EbookInfo> resBookInfoList;
        EbookInfo bookinfo;
        int rowNumber = ui->tableWidgetModify->rowCount();
        for(int i = 0; i < rowNumber; ++i)
        {
            bookinfo.numId = ui->tableWidgetModify->item(i,0)->text().toInt();
            bookinfo.bookname = ui->tableWidgetModify->item(i,1)->text();
            bookinfo.booktype = ui->tableWidgetModify->item(i,2)->text();
            bookinfo.booklabel = ui->tableWidgetModify->item(i,3)->text();
            bookinfo.bookpath = ui->tableWidgetModify->item(i,4)->text();
            bookinfo.bookdir = ui->tableWidgetModify->item(i,5)->text();
            bookinfo.author = ui->tableWidgetModify->item(i,6)->text();
            bookinfo.publisher = ui->tableWidgetModify->item(i,7)->text();
            bookinfo.publishyear = ui->tableWidgetModify->item(i,8)->text();
            bookinfo.bookisbn = ui->tableWidgetModify->item(i,9)->text();
            bookinfo.bookintroduction = ui->tableWidgetModify->item(i,10)->text();
            resBookInfoList.append(bookinfo);
        }

        emit sendModifyTableData(resBookInfoList, rowNumber);
        this->hide();
    }
}

void BatchModify::on_ptnCancelModify_clicked()
{
    this->hide();
}
